package com.ficus.road.maintain.controller;

import com.ficus.road.maintain.converter.MockHitConverter;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.oss.AliyunOssResult;
import com.ficus.road.maintain.service.HitService;
import com.ficus.road.maintain.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author hao.zhou1
 * @Since 2021/11/30 下午1:47
 */
@RestController
@RequestMapping
public class MockHitController {

    public static final String PICTURE_PATH = "mock";
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private HitService hitService;

    @Autowired
    private MockHitConverter mockHitConverter;

    @RequestMapping(value = "/mockhits",
            method = RequestMethod.POST)
    public ResponseEntity<IdResult> createMockHit(@Valid @RequestBody MockHitCreateRequest mockHit) {
        Hit hit = mockHitConverter.convert(mockHit);
        String id = hitService.createHit(hit);
        //回传一个序列化的id
        return new ResponseEntity<>(IdResult.builder().id(id).build(), HttpStatus.OK);
    }

    @RequestMapping(value = "/mockhits",
            method = RequestMethod.GET)
    public ResponseEntity<List<MockHitDetailResponse>> listMockHits(@Min(0) @Valid @RequestParam(defaultValue = "0") Integer start,
                                                                    @Min(1) @Valid @RequestParam(defaultValue = "100") Integer limit) {
        List<Hit> hits = hitService.queryMockHits(start, limit);
        List<MockHitDetailResponse> mockHitDetailResponseList = hits.stream().map(item -> mockHitConverter.parseHit(item)).collect(Collectors.toList());
        //回传一个序列化的id
        return new ResponseEntity<>(mockHitDetailResponseList, HttpStatus.OK);
    }

    @RequestMapping(value = "/mockhits/delete",
            method = RequestMethod.POST)
    public ResponseEntity<List<IdResult>> deleteMockHits(@RequestBody List<String> ids) {
        ids.parallelStream().forEach(item -> hitService.delete(item));
        List<IdResult> idResults = ids.stream().map(item -> IdResult.builder().id(item).build()).collect(Collectors.toList());
        return new ResponseEntity<>(idResults, HttpStatus.OK);
    }

    @RequestMapping(value = "/mockhits/{id}",
            method = RequestMethod.GET)
    public ResponseEntity<MockHitDetailResponse> getMockhit(@PathVariable("id") String id) {
        Hit hit = hitService.getHit(id);
        MockHitDetailResponse mockHitDetailResponse = mockHitConverter.parseHit(hit);
        //回传一个序列化的id
        return new ResponseEntity<>(mockHitDetailResponse, HttpStatus.OK);
    }

    @RequestMapping(value = "/mockhits",
            method = RequestMethod.PATCH)
    public ResponseEntity<List<IdResult>> updateMockhit(@Valid @RequestBody List<MockHitUpdateRequest> mockHitUpdateRequests) {
        hitService.updateMockHits(mockHitUpdateRequests);
        List<IdResult> idResultList = mockHitUpdateRequests.stream().map(item -> IdResult.builder().id(item.getId()).build()).collect(Collectors.toList());
        //回传一个序列化的id
        return new ResponseEntity<>(idResultList, HttpStatus.OK);
    }

    @RequestMapping(value = "/picture/upload",
            method = RequestMethod.POST)
    public ResponseEntity<UrlResult> uploadPicture(@RequestParam("arguments") MultipartFile image) throws Exception {
        byte[] bytes = image.getBytes();
        String format = sdf.format(new Date());
        AliyunOssResult ossResult = aliyunOSSUtil.uploadImg(PICTURE_PATH + "/" + format + "/" + UUID.randomUUID() + ".jpg", bytes);
        String url = ossResult.getUrl();
        UrlResult urlResult = UrlResult.builder().url(url).build();
        // 回传一个序列化的id
        return new ResponseEntity<>(urlResult, HttpStatus.OK);
    }
}
